package com.chenwut.threadDemo.exercise_1;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThreadLock03 extends Thread {
    static int ticket = 0;

    static Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            try {
                lock.lock();
                if (ticket == 100) {
                    break;
                }
                Thread.sleep(10);
                ticket++;
                System.out.println(getName() + " 发放了 票：" + ticket + " 剩余票数为： " + (100 - ticket));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }
    }
}
